﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eCommMongo.Core;
using eCommMongo.Core.Domain.Products;
using eCommMongo.Service.Pictures;
using eCommMongo.Service.Products;
using eCommMongo.Service.ShoppingCarts;
using eCommMongo.Web.Models.ProductVariants;
using eCommMongo.Web.Models.Products;
using eCommMongo.Web.Models.SellProducts;

namespace eCommMongo.Web.Controllers
{
    public class SellProductController : Controller
    {
        private readonly IShoppingCartService _shoppingCartService;
        private readonly IProductService _productService;
        private readonly IWorkContext _workContext;
        private IPictureService _pictureService;
        private IProductSearchService _productSearchService;

        public SellProductController(
            IProductSearchService productSearchService,
            IShoppingCartService shoppingCartService, IProductService productService, IWorkContext workContext, IPictureService pictureService)
        {
            _productSearchService = productSearchService;
            _shoppingCartService = shoppingCartService;
            _productService = productService;
            _workContext = workContext;
            _pictureService = pictureService;
        }

        //public ActionResult NewProducts()
        //{
        //    var products = _productSearchService.SearchProducts(string , 0, 0, string.Empty, ProductSortingEnum.CreatedOn, 0, 30);
        //    var pModels = yiCommerce.Mapper.ProductMapper.PrepareProductOverviewModels(products);
        //    ViewBag.Model = pModels;
        //    return View();
        //}

        public ActionResult Product(string productId)
        {

            var product = _productService.GetProductById(productId);
            if (product == null || product.Deleted || !product.Published)
                return Redirect("/");

            var model = PrepareProductDetailsPageModel(product);

            ViewBag.Model = model;
            return View("SingleProduct");
        }





        protected ProductDetailsModel PrepareProductDetailsPageModel(Product product)
        {

            if (product == null)
                throw new ArgumentNullException("product");

            var model = new ProductDetailsModel()
            {
                Id = product.Id.ToString(),
                Name = product.ProductName,
                ShortDescription = product.ShortDescription,
                FullDescription = product.FullDescription,

                PictureModels = new List<PictureModel>(),

                ProductVariantModels = new List<ProductDetailsModel.ProductVariantModel>(),
                SpecificationAttributeModels = new List<ProductSpecificationAttributeModel>(),

            };

            var productPictures = product.ProductPictures;

            if (productPictures.Count > 0)
            {
                //default picture
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl = _pictureService.GetPictureUrl(productPictures.FirstOrDefault().Picture, 300, false),
                    FullSizeImageUrl = _pictureService.GetPictureUrl(productPictures.FirstOrDefault().Picture, 0, false),
                    Title = model.Name,
                };
                //all pictures
                foreach (var productPicture in productPictures)
                {
                    model.PictureModels.Add(new PictureModel()
                    {
                        ImageUrl = _pictureService.GetPictureUrl(productPicture.Picture, 300, false),
                        FullSizeImageUrl = _pictureService.GetPictureUrl(productPicture.Picture, 0, false),
                        Title = model.Name,
                    });
                }
            }
            else
            {
                //no images. set the default one
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl = _pictureService.GetDefaultPictureUrl(300),
                    FullSizeImageUrl = _pictureService.GetDefaultPictureUrl(0),
                    Title = model.Name,
                };
            }

            //product variants
            foreach (var variant in product.ProductVariants)
            {

                var pvm = PrepareProductVariantModel(variant);
                model.ProductVariantModels.Add(pvm);
            }

            foreach (var attribute in product.ProductSpecificationAttributes)
            {
                var m = PrepareProductSpecificationAttributeModel(attribute);
                model.SpecificationAttributeModels.Add(m);
            }

            return model;
        }

        private ProductSpecificationAttributeModel PrepareProductSpecificationAttributeModel(SpecificationAttributeOption attribute)
        {
            var model = new ProductSpecificationAttributeModel()
            {
                AttributeName = attribute.SpecificationAttributeName,
                AttributeOptionName = attribute.SpecificationAttributeOptionName,
            };
            return model;
        }

        protected ProductDetailsModel.ProductVariantModel PrepareProductVariantModel(
            ProductVariant productVariant)
        {
            if (productVariant == null)
                throw new ArgumentNullException("productVariant");

            var model = new ProductDetailsModel.ProductVariantModel();
            model.Id = productVariant.Id.ToString();
            model.Name = productVariant.Name;
            model.Sku = productVariant.Sku;
            model.Description = productVariant.Description;
            model.StockQuantity = productVariant.StockQuantity;


            model.PictureModel = new PictureModel
            {
                FullSizeImageUrl = _pictureService.GetPictureUrl(productVariant.Picture, 0, false),
                ImageUrl = _pictureService.GetPictureUrl(productVariant.Picture, 300, false),
                Title = productVariant.Name
            };

            model.Price = productVariant.Price;

            model.AddToCart = new AddToCartModel
            {
                ProductVariantId = productVariant.Id.ToString()
            };
            model.ProductVariantAttributes = new List<ProductDetailsModel.ProductVariantModel.ProductVariantAttributeModel>();

            var productVariantAttributes = productVariant.ProductVariantAttributes;
            foreach (var attribute in productVariantAttributes)
            {
                var pvaModel = new ProductDetailsModel.ProductVariantModel.ProductVariantAttributeModel()
                {

                    Name = attribute.ProductAttribute.Name,
                    Description = attribute.ProductAttribute.Description,

                    AttributeControlType = attribute.AttributeControlType,
                    ProductVariantAttributeValueModels = attribute.ProductVariantAttributeValues == null ? new List<ProductVariantAttributeValueModel>() :
                    attribute.ProductVariantAttributeValues.Select(c => new ProductVariantAttributeValueModel()
                    {
                        Id = c.Id.ToString(),
                        Name = c.Name,
                        DisplayOrder = c.DisplayOrder

                    }).ToList(),
                };
                model.ProductVariantAttributes.Add(pvaModel);
            }
            return model;
        }


        //public JsonResult AddProductVariantToCart(int productVariantId, int quantity, IList<ProductVariantAttributeValue> attributeValues)
        //{

        //    var pv = _productService.GetProductVariantById(productVariantId);

        //    var selectedAttributes = string.Empty;

        //    _shoppingCartService.AddToCart(_workContext.CurrentCustomer.Id, pv, selectedAttributes, quantity);

        //    return null;

        //}
    }
}
